﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VirtualKeyboard.Service.Commands;

namespace VirtualKeyboard.UI
{
    /// <summary>
    /// InputKeyControl.xaml 的交互逻辑
    /// </summary>
    public partial class InputKeyControl
    {
        public static readonly DependencyProperty ShortcutKeyProperty
            = DependencyProperty.Register("ShortcutKey", typeof (string), typeof (InputKeyControl),
                                          new PropertyMetadata(null, ShortcutKeyChangedCallBack));

        private static void ShortcutKeyChangedCallBack(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var inputKeyControl = (InputKeyControl) obj;
            inputKeyControl.keyValue.Text = (string) e.NewValue;
        }

        public string ShortcutKey
        {
            get { return (string) GetValue(ShortcutKeyProperty); }
            set { SetValue(ShortcutKeyProperty, value); }
        }

        private static readonly HashSet<Key> KeyHash = new HashSet<Key>
                                                           {
                                                               Key.LeftAlt,
                                                               Key.LeftCtrl,
                                                               Key.LeftShift,
                                                               Key.RightAlt,
                                                               Key.RightCtrl,
                                                               Key.RightShift,
                                                           };

        public InputKeyControl()
        {
            InitializeComponent();
        }

        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            e.Handled = true;

            if (KeyHash.Contains(e.Key)) return;
            if ((e.KeyboardDevice.Modifiers & ModifierKeys.Windows) == ModifierKeys.Windows) return;

            if (e.KeyboardDevice.Modifiers == ModifierKeys.None)
            {
                UpdateShortcutKey(e.Key.GetKeyString());
            }
            else
            {
                var stringBuilder = new StringBuilder();
                if ((e.KeyboardDevice.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
                    stringBuilder.Append("Control+");
                if ((e.KeyboardDevice.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
                    stringBuilder.Append("Alt+");
                if ((e.KeyboardDevice.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
                    stringBuilder.Append("Shift+");
                stringBuilder.Append(e.Key.GetKeyString());
                UpdateShortcutKey(stringBuilder.ToString());
            }
            base.OnPreviewKeyDown(e);
        }

        private void UpdateShortcutKey(string shortcutKey)
        {
            keyValue.Text = shortcutKey;
            ShortcutKey = shortcutKey;
        }
    }

    internal static class KeyExtension
    {
        public static string GetKeyString(this Key key)
        {
            if (key == Key.Return) return "Enter";

            return key.ToString();
        }
    }
}
